﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Data;

namespace Top.PointStore.PointAddService
{
	class Program
	{
		static void Main(string[] args)
		{
            PointAdd().Wait();
		}

        public static async Task<object> PointAdd()
		{
			string addStr = string.Empty;
			using (StreamReader sr = new StreamReader(@"D:\pointadd.txt"))
			{
				addStr = sr.ReadToEnd();
			}
			string[] addUser = addStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
			List<Phone_Point> list = new List<Phone_Point>();
			for (int i = 0; i < addUser.Length; i++)
			{
				string[] pp = addUser[i].Split(new string[]{","},StringSplitOptions.RemoveEmptyEntries);
				list.Add(new Phone_Point()
				{
					Phone = pp[0],
					Point = Convert.ToInt32(pp[1])
				});
			}
            foreach (var item in list)
            {
                await AddPointByPhone(item, "top.activity.add");
            }
            Console.WriteLine("全部添加完毕！");
            Console.ReadKey();
            return null;
		}
        public static int num = 0;
        public static async Task<object> AddPointByPhone(Phone_Point model,string code)
		{
            ++num;
            using (var db = new DbTour())
			{
				string sql = @"
					select 
						d.*
						FROM
							T_Broker a
						LEFT JOIN T_OAuthInfo b ON a.Kid = b.F_BrokerKid
						LEFT JOIN T_PointMain_new d ON b.F_UserToken = d.F_UserToken
						where a.F_Phone = {0}
				";
                List<PointMain> pmainList =await db.Sql(sql, model.Phone).ToList<PointMain>();
                PointMain pmain = pmainList.FirstOrDefault();
                Console.WriteLine("添加积分-{2} 手机号：{0}\t积分：{1}", model.Phone, model.Point, num);
                string sql2 = @"
                    update T_PointMain_new
                    set F_PointAll = F_PointAll+{1},
                    F_PointBalance = F_PointBalance+{1}
                    WHERE KID = {0}";
                db.Sql(sql2, pmain.Kid, model.Point).ExecuteNonQuery().Wait();
                string sql3 = @"
                insert into T_PointDetail_new (F_PointId,F_BuildingKid,F_PointCode,F_Point,F_AddTime,F_PointSource)
                values({0},
                {1},
                {2},
                {3},
                now(),
                300);
                 ";
                await db.Sql(sql3, pmain.Kid, 0, code, model.Point).ExecuteNonQuery();
                Console.WriteLine("添加积分-{2} 手机号：{0}\t积分：{1}\t 完毕！", model.Phone, model.Point, num);
			}
            return null;
		}
	}

	public class Phone_Point
	{
		public string Phone { get;set;}
		public int Point { get; set; }
	}

	public class PointMain
	{
		public int Kid { get; set; }
		/// <summary>
		/// 用户
		/// </summary>
		public string F_UserToken { get; set; }
		/// <summary>
		/// 累计积分
		/// </summary>
		public int F_PointAll { get; set; }
		/// <summary>
		/// 已兑换积分
		/// </summary>
		public int F_PointCash { get; set; }
		/// <summary>
		/// 积分余额
		/// </summary>
		public int F_PointBalance { get; set; }
	}
}
